//
//  XmColorView.m
//  ColorTest
//
//  Created by Rowling on 2017/6/22.
//  Copyright © 2017年 晓明罗. All rights reserved.
//

#import "XmColorView.h"
#import <QuartzCore/QuartzCore.h>

@interface XmColorView()
@property (nonatomic,strong)UIImageView * imageView;
@property (nonatomic,strong) UIView *Mview;
@end

@implementation XmColorView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
}

-(void)layoutSubviews {
    [self setUI];
}

-(UIView *)Mview {
    if (_Mview == nil) {
        _Mview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    }
    return _Mview;
}

-(void)setUI {
    if (self.imageView == nil) {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:imageView];
        imageView.image = [UIImage imageNamed:@"nko_colormap"];
        self.imageView = imageView;
    }
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    self.Mview.backgroundColor = [self colorOfPoint:location];
    UIColor * color = [self colorOfPoint:location];
    [self addSubview:self.Mview];
    if (self.colorBlock) {
        self.colorBlock(color);
    }
}

//
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    self.Mview.backgroundColor = [self colorOfPoint:location];
    [self addSubview:self.Mview];
    UIColor * color = [self colorOfPoint:location];
    if (self.colorBlock) {
        self.colorBlock(color);
    }
}

-(void)dealloc {
    
}

@end
